<?php

FLEA::loadClass('FLEA_Controller_Action');

class Controller_Admin_menu extends FLEA_Controller_Action
{

	/**
	 * $md_menu
	 * @var Model_MenuManager
	 */
	var $md_menu;
	
	function Controller_Admin_menu()
	{
		$this->md_menu =& FLEA::getSingleton('Model_MenuManager');
	}
	
	function actionIndex()
	{
		redirect($this->_url('menulist'));		
	}
	
	/**
	 * 菜单列表页，具备菜单CRUD的功能
	 */
	function actionMenuList(){
		
		//组织数据
		$viewdata = array(
			// 菜单项列表
			'menuList' => $this->md_menu->menuList(),
			// 权限组列表
			'perGroupList' => $this->md_menu->permissionGroupList(),
			// title标签
			'title' => '菜单管理',
			// 当前菜单标签
			'menu_menu' => 'id="current"',
	    );
	    
		$this->_executeView('admin/Websetup_menu.html', $viewdata);	
	}
	
	/**
	 * 创建一个新的菜单项
	 */
	function actionMenuCreate(){
		$this->_jumpto($this->md_menu->menuCreate($_POST));
	}
	
	/**
	 * 删除一个菜单项
	 */
	function actionMenuRemove(){
		echo $this->md_menu->menuRemove($_GET['menu_id']);
	}
	
	/**
	 * ajax : $_GET获取的permission_group_id的值返回permission数组json串
	 */
	function actionGetPerByGroup(){
		$permission_group_id = isset($_GET['group_id']) ? $_GET['group_id'] : null;
		echo json_encode($this->md_menu->permissionList($permission_group_id));
	}
	
	/**
	 * ajax : 返回menu的json串
	 */
	function actionAjaxMenuTree(){
		echo json_encode($this->md_menu->menuList($_GET['parent_id']));
	}
	
	/**
	 * 更新一个菜单项
	 */
	function actionMenuUpdate(){
		$this->_jumpto($this->md_menu->menuCreate($_POST));
	}
	
	
	/**
	 * 判断错误并实施页面跳转
	 *
	 * @param boolin $stat
	 * @param string $err_text
	 * @param url $to_url
	 * @param url $err_url
	 */
	function _jumpto($stat, $err_text=null, $to_url=null, $err_url=null)
	{
		$err_text = isset($err_text) ? $err_text : "有错误发生！";
		$to_url = isset($to_url) ? $to_url : $this->_url('menulist');
		$err_url = isset($err_url) ? $err_url : $to_url;
		
		if($stat){
			redirect($to_url);
		}else{
			js_alert($err_text, $err_url);
		}
	}
	
	
	
}

?>